package com.qf.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DeadLetterConfig {
    // 普通交换机和普通队列
    public final static String NORMAL_EXCHANGE = "normal_exchange";
    public final static String NORMAL_QUEUE = "normal_queue";

    // 死信交换机死信队列
    public final static String DEAD_EXCHANGE = "dead_exchange";
    public final static String DEAD_QUEUE = "dead_queue";


    @Bean
    public Exchange normalExchange(){
        return ExchangeBuilder.fanoutExchange(NORMAL_EXCHANGE).build();
    }

    @Bean   // 创建普通队列,并绑定死信交换机
    public Queue normalQueue(){
//        设置消息的生存时间
//        return QueueBuilder.durable(NORMAL_QUEUE).withArgument("x-dead-letter-exchange",DEAD_EXCHANGE).withArgument("x-message-ttl",5000).build();
        return QueueBuilder.durable(NORMAL_QUEUE).withArgument("x-dead-letter-exchange",DEAD_EXCHANGE).build();
    }

    @Bean
    public Binding normalBinding(Queue normalQueue,Exchange normalExchange){
        return BindingBuilder.bind(normalQueue).to(normalExchange).with("").noargs();
    }



    @Bean
    public Exchange deadExchange(){
        return ExchangeBuilder.fanoutExchange(DEAD_EXCHANGE).build();
    }


    @Bean
    public Queue deadQueue(){
        return QueueBuilder.durable(DEAD_QUEUE).build();
    }

    @Bean
    public Binding deadBinding(Queue deadQueue,Exchange deadExchange){
        return BindingBuilder.bind(deadQueue).to(deadExchange).with("").noargs();
    }


}
